#include <stdlib.h>

#include "pyramid.h"

struct eltPyramid{

  struct eltPyramid* next;
  struct eltPyramid* prev;
  struct maskedImage* image;
  int level;
};

struct fifoPyramid{

  struct eltPyramid* first;

};

/* Push an image (as a pyramid element) to the desired pyramid fifo structure */
void pushImage(struct fifoPyramid* fifo,struct maskedImage* elt) {

  struct eltPyramid* newElt = malloc(sizeof(struct eltPyramid));

  newElt->image = elt;
  newElt->next = NULL;

  struct eltPyramid* tmp = fifo->first;

  if(tmp == NULL) {
    newElt->level = 0;
    fifo->first = newElt;
    newElt->prev = NULL;
  }
    
  int level;

  for(tmp; tmp->next != NULL; tmp = tmp->next)
    level++;
  
  newElt->level = level;
  tmp->next = newElt;
  newElt->prev = tmp;
}

/* pop the last element (as a pyramid element) of the desired fifo structure */
struct eltPyramid* popImage(struct fifoPyramid* fifo) {

  struct eltPyramid* tmp = fifo->first;

  if(tmp == NULL) return NULL;

  for(tmp;tmp->next != NULL; tmp = tmp->next);

  (tmp->prev)->next = NULL;

  return tmp;
}

/* Counts the number of elements in the fifo structure */
int sizePyramid(struct fifoPyramid* fifo) {

  int count = 0;

  struct eltPyramid* tmp = fifo->first;

  if(tmp == NULL) return 0;

  for(tmp;tmp->next != NULL; tmp = tmp->next) count++;

  return count;
}
